<?php
/**
 * Created by PhpStorm.
 * User: zy-l
 * Date: 2019/6/19
 * Time: 1:23 AM
 */

namespace App\HttpController\Admin;

use App\HttpController\AdminBase;
use App\Model\InfoAdv;
use EasySwoole\Validate\Validate;

/**
 * 广告管理
 * Class Adv
 * @package App\HttpController\Admin
 */
class Adv extends AdminBase
{

    /**
     * 公告模型
     * @return InfoAdv
     */
    public function getAdvModel()
    {
        return new InfoAdv();
    }

    /**
     * 首页
     * @return string|void
     */
    public function index()
    {
        if ($this->isPost()) {
            return $this->writePageJson('获取列表数据', $this->getAdvModel()->getPageAll([]));
        }
        return $this->fetch('index');
    }

    /**
     * 新增
     */
    public function add()
    {

        if ($this->isPost()) {
            $data=$this->postParams();
            $data['start_time']=strtotime($this->postParam('start_time'));
            $data['end_time']=strtotime($this->postParam('end_time'));
            return $this->writeJson('操作成功', $this->getAdvModel()->updateOrCreateData($data, ["id" => $this->postParam("id")]));
        }
        return $this->fetch('add');
    }

    /**
     * 删除
     * @return bool
     */
    public function del(){
        return $this->writeJson('删除成功',$this->getAdvModel()->delData(['id'=>$this->postParam('id')]));
    }

    /**
     * 编辑
     * @return bool|string|void
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function edit()
    {
        $id = $this->getParam('id');
        if ($this->isPost()) {
            return $this->writeJson('操作成功', $this->getAdvModel()->updateOrCreateData($this->postParams(), ["id" => $this->postParam("id")]));
        }
        $info = $this->getAdvModel()->findOne(["id" => $id]);
        print_r($info);
        $this->assign(["data" => json_encode($info)]);
        return $this->fetch("edit");
    }

    /**
     * 提交参数验证
     * @param string|null $action
     * @return Validate|null
     */
    protected function getValidateRule(?string $action): ?Validate
    {
        $validate = null;
        return $validate;
    }
}